package com.happok.onlineclass.usermanagement.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.happok.onlineclass.usermanagement.user.dao.UserPermissionDAO;
import com.happok.onlineclass.usermanagement.user.dataobject.UserPermissionDO;
import com.happok.onlineclass.usermanagement.user.dto.OrgTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/8/16 17:11
 */
@Service
public class UserPermissionDaoServiceImpl implements UserPermissionDaoService {

    @Autowired
    private UserPermissionDAO userPermissionDao;

    @Override
    public void insert(UserPermissionDO userPermissionDO) {
        userPermissionDao.insert(userPermissionDO);
    }

    @Override
    public void update(UserPermissionDO userPermissionDO) {
        LambdaUpdateWrapper<UserPermissionDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserPermissionDO::getUserId, userPermissionDO.getUserId());
        if (!ObjectUtils.isEmpty(userPermissionDO.getOrgId())) {
            updateWrapper.eq(UserPermissionDO::getOrgId, userPermissionDO.getOrgId());
        }
        updateWrapper.eq(UserPermissionDO::getOrgType, userPermissionDO.getOrgType());
        UserPermissionDO updatePermission = new UserPermissionDO();
        updatePermission.setPermission(userPermissionDO.getPermission());
        userPermissionDao.update(updatePermission, updateWrapper);
    }

    @Override
    public void delete(UserPermissionDO userPermissionDO) {
        LambdaQueryWrapper<UserPermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPermissionDO::getUserId, userPermissionDO.getUserId());
        if (!ObjectUtils.isEmpty(userPermissionDO.getOrgId())) {
            queryWrapper.eq(UserPermissionDO::getOrgId, userPermissionDO.getOrgId());
        }
        queryWrapper.eq(UserPermissionDO::getOrgType, userPermissionDO.getOrgType());
        userPermissionDao.delete(queryWrapper);
    }

    @Override
    public Boolean isUserPermissionExist(Integer userId, Integer orgId, String orgType) {
        LambdaQueryWrapper<UserPermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPermissionDO::getUserId, userId);
        if (!ObjectUtils.isEmpty(orgId)) {
            queryWrapper.eq(UserPermissionDO::getOrgId, orgId);
        }
        queryWrapper.eq(UserPermissionDO::getOrgType, orgType);
        UserPermissionDO userPermissionDO = userPermissionDao.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(userPermissionDO)) {
            return false;
        }
        return true;
    }

    @Override
    public Map<Integer, List<GrantedAuthority>> getTrainOrgAuthorities(Integer userId) {
        List<UserPermissionDO> userPermissionDOS = listUserPermission(userId, OrgTypeEnum.TRAINING_ORG.getOrgType());
        Map<Integer, List<GrantedAuthority>> authorizedMap = new HashMap<>();
        if (ObjectUtils.isEmpty(userPermissionDOS)) {
            return authorizedMap;
        }
        for (UserPermissionDO userPermissionDO : userPermissionDOS) {
            List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            for (String permission : Arrays.asList(userPermissionDO.getPermission().split(","))) {
                grantedAuthorities.add(new SimpleGrantedAuthority(permission));
            }
            authorizedMap.put(userPermissionDO.getOrgId(), grantedAuthorities);
        }
        return authorizedMap;
    }

    @Override
    public Map<Integer, List<String>> trainOrgAuthorities(Integer userId) {
        Map<Integer, List<String>> authorizedMap = new HashMap<>();
        List<UserPermissionDO> userPermissionDOS = listUserPermission(userId, OrgTypeEnum.TRAINING_ORG.getOrgType());
        if (ObjectUtils.isEmpty(userPermissionDOS)) {
            return authorizedMap;
        }
        for (UserPermissionDO userPermissionDO : userPermissionDOS) {
            List<String> grantedAuthorities = new ArrayList<>();
            for (String permission : Arrays.asList(userPermissionDO.getPermission().split(","))) {
                grantedAuthorities.add(permission);
            }
            authorizedMap.put(userPermissionDO.getOrgId(), grantedAuthorities);
        }
        return authorizedMap;
    }

    @Override
    public Map<Integer, List<GrantedAuthority>> getEnterpriseAuthorities(Integer userId) {
        List<UserPermissionDO> userPermissionDOS = listUserPermission(userId, OrgTypeEnum.ENTERPRISE.getOrgType());
        Map<Integer, List<GrantedAuthority>> authorizedMap = new HashMap<>();
        if (ObjectUtils.isEmpty(userPermissionDOS)) {
            return authorizedMap;
        }
        for (UserPermissionDO userPermissionDO : userPermissionDOS) {
            List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            for (String permission : Arrays.asList(userPermissionDO.getPermission().split(","))) {
                grantedAuthorities.add(new SimpleGrantedAuthority(permission));
            }
            authorizedMap.put(userPermissionDO.getOrgId(), grantedAuthorities);
        }
        return authorizedMap;
    }

    @Override
    public Map<Integer, List<String>> enterpriseAuthorities(Integer userId) {
        List<UserPermissionDO> userPermissionDOS = listUserPermission(userId, OrgTypeEnum.ENTERPRISE.getOrgType());
        Map<Integer, List<String>> authorizedMap = new HashMap<>();
        if (ObjectUtils.isEmpty(userPermissionDOS)) {
            return authorizedMap;
        }
        for (UserPermissionDO userPermissionDO : userPermissionDOS) {
            List<String> grantedAuthorities = new ArrayList<>();
            for (String permission : Arrays.asList(userPermissionDO.getPermission().split(","))) {
                grantedAuthorities.add(permission);
            }
            authorizedMap.put(userPermissionDO.getOrgId(), grantedAuthorities);
        }
        return authorizedMap;
    }

    @Override
    public List<GrantedAuthority> getPlatformAuthorities(Integer userId) {
        UserPermissionDO userPermissionDO = getUserPermission(userId, OrgTypeEnum.PLATFORM.getOrgType());
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        if (ObjectUtils.isEmpty(userPermissionDO)) {
            return grantedAuthorities;
        }
        for (String permission : Arrays.asList(userPermissionDO.getPermission().split(","))) {
            grantedAuthorities.add(new SimpleGrantedAuthority(permission));
        }
        return grantedAuthorities;
    }

    @Override
    public List<String> platformAuthorities(Integer userId) {
        UserPermissionDO userPermissionDO = getUserPermission(userId, OrgTypeEnum.PLATFORM.getOrgType());
        List<String> grantedAuthorities = new ArrayList<>();
        if (ObjectUtils.isEmpty(userPermissionDO)) {
            return grantedAuthorities;
        }
        for (String permission : Arrays.asList(userPermissionDO.getPermission().split(","))) {
            grantedAuthorities.add(permission);
        }
        return grantedAuthorities;
    }

    private UserPermissionDO getUserPermission(Integer userId, String orgType) {
        LambdaQueryWrapper<UserPermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPermissionDO::getUserId, userId);
        queryWrapper.eq(UserPermissionDO::getOrgType, orgType);
        return userPermissionDao.selectOne(queryWrapper);
    }

    private List<UserPermissionDO> listUserPermission(Integer userId, String orgType) {
        LambdaQueryWrapper<UserPermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPermissionDO::getUserId, userId);
        queryWrapper.eq(UserPermissionDO::getOrgType, orgType);
        return userPermissionDao.selectList(queryWrapper);
    }
}